﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Helpers;
using System.Web;

namespace MyToolKits
{
    public static class WebImageUtil
    {
        public static WebImage GetImageFromRequest(HttpRequestBase request, string fileName=null)
        {

            if (request.Files.Count == 0)
            {
                return null;
            }

            try
            {
                HttpPostedFileBase postedFile;
                if (fileName == null)
                {
                    postedFile = request.Files[0];
                }
                else
                {
                    postedFile = request.Files[fileName];
                }
                var image = new WebImage(postedFile.InputStream)
                {
                    FileName = postedFile.FileName
                };
                return image;
            }
            catch
            {
                // The user uploaded a file that wasn't an image or an image format that we don't understand
                return null;
            }
        }
    }
}
